﻿<%@ Page Language="VB" Debug="true" %>
<%@ Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI" Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral, PublicKeyToken=6f763c9966660626" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    Sub Page_Load()
        Dim ConnectionString As String = ConfigurationSettings.AppSettings("strcnn")
        Dim strsql As String
        Dim myConnection As New OleDbConnection(ConnectionString)
        Dim myCommand as New OleDbCommand
        Dim myDataReader as OleDbDataReader
        myConnection.Open()
        myCommand.Connection = myConnection
    
        select(Request.Form("acao"))
        case "excluir":
            Dim primeiro as Boolean = true
            for each i as String in Request.Form
                if i <> "acao" and i <> "RazaoSocial" then
                    if primeiro then
                        strsql = "delete * from fabrica where ID=" & i
                        primeiro = false
                    else
                        strsql = strsql & " or ID=" & i
                    End if
                End if
            next
            if strsql <> "" then
                myCommand.CommandText = strsql
                myCommand.ExecuteNonQuery()
            End if
        case "inserir":
            strsql = "Insert INTO Fabrica ( RazaoSocial, Endereco, Bairro, CEP, Cidade, Estado, Telefone, Fax, Email, CNPJ, InscricaoEstadual, Contato, Observacao ) Values ('" & Request.Form("RazaoSocial") & "', '" & Request.Form("Endereco") & "', '" & Request.Form("Bairro") & "', '" & Request.Form("CEP") & "', '" & Request.Form("Cidade") & "', '" & Request.Form("Estado") & "', '" & Request.Form("Telefone") & "', '" & Request.Form("FAX") & "', '" & Request.Form("Email") & "', '" & Request.Form("CNPJ") & "', '" & Request.Form("InscricaoEstadual") & "', '" & Request.Form("Contato") & "', '" & Request.Form("Observacao") & "')"
            myCommand.CommandText = strsql
            myCommand.ExecuteNonQuery()
        case "alterar":
            strsql = "update fabrica set RazaoSocial='" & Request.Form("RazaoSocial") & "', Endereco='" & Request.Form("Endereco") & "', Bairro='" & Request.Form("Bairro") & "', CEP='" & Request.Form("CEP") & "', Cidade='" & Request.Form("Cidade") & "', Estado='" & Request.Form("Estado") & "', Telefone='" & Request.Form("Telefone") & "', FAX='" & Request.Form("FAX") & "', Email='" & Request.Form("Email") & "', CNPJ='" & Request.Form("CNPJ") & "', InscricaoEstadual='" & Request.Form("InscricaoEstadual") & "', Contato='" & Request.Form("Contato") & "', Observacao='" & Request.Form("Observacao") & "' where id=" & Request.Form("id")
            myCommand.CommandText = strsql
            myCommand.ExecuteNonQuery()
        End select
    
        select(Request("acao"))
        case "exibir":
            dim possuiconta as boolean = true
            strsql = "select * from fabrica, conta where fabrica.id=" & Request.QueryString("id") & " and fabrica.id=conta.idfabrica order by conta.banco, conta.agencia, conta.conta asc"
            myCommand.CommandText = strsql
            myDataReader = myCommand.ExecuteReader()
            if not myDataReader.Read() then
                myDataReader.Close()
                strsql = "select * from fabrica where fabrica.id=" & Request.QueryString("id")
                myCommand.CommandText = strsql
                myDataReader = myCommand.ExecuteReader()
                myDataReader.Read()
                possuiconta = false
            end if
            Painel.Text = "<h3>Exibir:</h3><table border='0' cellspacing='0' cellpadding='4'>"
            Painel.Text = Painel.Text & "<tr><td>Razão social:</td><td>" & myDataReader("RazaoSocial") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Endereço:</td><td>" & myDataReader("Endereco") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Bairro:</td><td>" & myDataReader("Bairro") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>CEP:</td><td>" & myDataReader("CEP") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Cidade:</td><td>" & myDataReader("Cidade") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Estado:</td><td>" & myDataReader("Estado") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Telefone:</td><td>" & myDataReader("Telefone") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Fax:</td><td>" & myDataReader("Fax") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>E-mail:</td><td><a href='enviaremail.aspx?email=" & myDataReader("Email") & "&razaosocial=" & myDataReader("RazaoSocial") & "'>" & myDataReader("Email") & "</a></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>CNPJ:</td><td>" & myDataReader("CNPJ") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Inscrição estadual:</td><td>" & myDataReader("InscricaoEstadual") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Contato:</td><td>" & myDataReader("Contato") & "</td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Observação:</td><td>" & myDataReader("Observacao") & "</td></tr>"
            if possuiconta then
                do
                    Painel.Text = Painel.Text & "<tr><td colspan='2'><hr></td></tr>"
                    Painel.Text = Painel.Text & "<tr><td>Banco:</td><td><a href='contafabrica.aspx?acao=exibir&id=" & myDataReader("conta.id") & "'>" & myDataReader("banco") & "</a></td></tr>"
                    Painel.Text = Painel.Text & "<tr><td>Agência:</td><td>" & myDataReader("agencia") & "</td></tr>"
                    Painel.Text = Painel.Text & "<tr><td>Conta:</td><td>" & myDataReader("conta") & "</td></tr>"
                loop until not myDataReader.Read()
            end if
            Painel.Text = Painel.Text & "<tr><td><form method='post' action='fabrica.aspx'><input type='hidden' name='acao' value='formalterar'><input type='hidden' name='id' value='" & Request.QueryString("id") & "'><input type='submit' value='Alterar'></form></td>"
            Painel.Text = Painel.Text & "<td><form action='fabrica.aspx'><input type='submit' value='Cancelar'></form></td></tr>"
            Painel.Text = Painel.Text & "</table>"
            myDataReader.Close()
        case "formalterar":
            strsql = "select * from fabrica where id=" & Request.Form("id")
            myCommand.CommandText = strsql
            myDataReader = myCommand.ExecuteReader()
            myDataReader.Read()
            Painel.Text = "<h3>Alterar:</h3><form method='post' action='fabrica.aspx'><input type='hidden' name='acao' value='alterar'><input type='hidden' name='id' value='" & myDataReader("ID") & "'><table border='0' cellspacing='0' cellpadding='4'>"
            Painel.Text = Painel.Text & "<tr><td>Razão social:</td><td><input name='RazaoSocial' value='" & myDataReader("RazaoSocial") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Endereço:</td><td><input name='Endereco' value='" & myDataReader("Endereco") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Bairro:</td><td><input name='Bairro' value='" & myDataReader("Bairro") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>CEP:</td><td><input name='CEP' value='" & myDataReader("CEP") & "' maxlength='10'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Cidade:</td><td><input name='Cidade' value='" & myDataReader("Cidade") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Estado:</td><td><input name='Estado' value='" & myDataReader("Estado") & "' maxlength='2'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Telefone:</td><td><input name='Telefone' value='" & myDataReader("Telefone") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Fax:</td><td><input name='Fax' value='" & myDataReader("Fax") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>E-mail:</td><td><input name='Email' value='" & myDataReader("Email") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>CNPJ:</td><td><input name='CNPJ' value='" & myDataReader("CNPJ") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Inscrição estadual:</td><td><input name='InscricaoEstadual' value='" & myDataReader("InscricaoEstadual") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Contato:</td><td><input name='Contato' value='" & myDataReader("Contato") & "'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Observação:</td><td><textarea name='Observacao' rows='5' cols='30'>" & myDataReader("Observacao") & "</textarea></td></tr>"
            Painel.Text = Painel.Text & "<tr><td><input type='submit' value='Alterar'></td><td><input type='reset' value='Limpar'></td></tr>"
            Painel.Text = Painel.Text & "</table></form>"
            myDataReader.Close()
        case else:
            Painel.Text = "<h3>Inserir:</h3><form method='post' action='fabrica.aspx'><input type='hidden' name='acao' value='inserir'><table border='0' cellspacing='0' cellpadding='4'>"
            Painel.Text = Painel.Text & "<tr><td>Razão social:</td><td><input name='RazaoSocial'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Endereço:</td><td><input name='Endereco'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Bairro:</td><td><input name='Bairro'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>CEP:</td><td><input name='CEP' maxlength='10'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Cidade:</td><td><input name='Cidade'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Estado:</td><td><input name='Estado' maxlength='2'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Telefone:</td><td><input name='Telefone'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Fax:</td><td><input name='Fax'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>E-mail:</td><td><input name='Email'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>CNPJ:</td><td><input name='CNPJ'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Inscrição estadual:</td><td><input name='InscricaoEstadual'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Contato:</td><td><input name='Contato'></td></tr>"
            Painel.Text = Painel.Text & "<tr><td>Observação:</td><td><textarea name='Observacao' rows='5' cols='30'></textarea></td></tr>"
            Painel.Text = Painel.Text & "<tr><td><input type='submit' value='Inserir'></td><td><input type='reset' value='Limpar'></td></tr>"
            Painel.Text = Painel.Text & "</table></form>"
        End Select
    
        Dim filterValue As String = Request.Form("RazaoSocial")
        if Request("Acao") = "inserir" or Request("Acao") = "alterar" then filterValue = ""
        If filterValue = "" Then
            strsql = "select ID, RazaoSocial, Telefone, Fax from fabrica order by RazaoSocial asc"
        Else
            strsql = "select ID, RazaoSocial, Telefone, Fax from fabrica where RazaoSocial like '" & filterValue & "%' order by RazaoSocial asc"
        End If
        myCommand.CommandText = strsql
        myDataReader = myCommand.ExecuteReader()
        Busca.Text = "<table width='100%' border='0' cellspacing='2' cellpadding='2'><tr bgcolor='#000080'><td><font color='#FFFFFF'><b>RazaoSocial</b></font></td><td><font color='#FFFFFF'><b>Telefone</b></font></td><td><font color='#FFFFFF'><b>Fax</b></font></td><td></td></tr>"
        Dim cor as Boolean
        while myDataReader.Read()
            Busca.Text = Busca.Text & "<tr bgcolor='" & iif(cor, "#ADD8E6", "#E0FFFF") & "'><td><a href='fabrica.aspx?acao=exibir&id=" & myDataReader("ID") & "'>" & myDataReader("RazaoSocial") & "</a></td><td>" & myDataReader("Telefone") & "</td><td>" & myDataReader("Fax") & "</td><td align='center'><input type='checkbox' name='" & myDataReader("ID") & "' value='true'></td></tr>"
            cor = not cor
        End while
        Busca.Text = Busca.Text & "</table>"
    
        myConnection.Close()
    End Sub

</script>
<html>
<head>
    <script language="vbscript">
    sub confirma()
        if msgbox("Confirmar exclusão?", vbYesNo) = vbYes then document.lista.submit()
    end sub
</script>
</head>
<body style="FONT-FAMILY: arial">
    <table bordercolor="navajowhite" cellspacing="0" cellpadding="2" width="950" align="center" bgcolor="floralwhite">
        <tbody>
            <tr>
                <td valign="center" align="middle" bgcolor="#ffdead" colspan="2">
                    <h2>Fábrica 
                    </h2>
                </td>
            </tr>
            <tr>
                <td valign="top" width="550">
                    <table border="0">
                        <tbody>
                            <tr>
                                <td>
                                    <h3>Pesquisar: 
                                    </h3>
                                    <form method="post">
                                        <table border="0">
                                            <tbody>
                                                <tr>
                                                    <td>
                                                        Razão social:</td>
                                                    <td>
                                                        <input name="RazaoSocial" /></td>
                                                </tr>
                                                <tr>
                                                    <td>
                                                    </td>
                                                    <td align="right">
                                                        <input type="submit" value="Pesquisar" /></td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </form>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <form name="lista" method="post">
                                        <input type="hidden" value="excluir" name="acao" />
                                        <%="<input type='hidden' name='RazaoSocial' value='" & Request.Form("RazaoSocial") & "'>"%><asp:Label id="Busca" runat="server">Busca</asp:Label>
                                        <br />
                                        <input onclick="vbscript:confirma()" type="button" value="Excluir" />
                                    </form>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
                <td valign="top" width="400">
                    <asp:Label id="Painel" runat="server">Painel</asp:Label></td>
            </tr>
        </tbody>
    </table>
</body>
</html>
